package com.briup.controller;

import com.alibaba.excel.EasyExcel;
import com.briup.bean.User;
import com.briup.bean.UserVO;
import com.briup.service.UserService;
import com.briup.utils.Result;
import com.briup.utils.UserCommonListener;
import lombok.SneakyThrows;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/users")
public class UserController {

	@Autowired
	private UserService userService;

	//导出数据到excel文件中
	@SneakyThrows //lombok 注解,使用它可以无需手动处理异常
	@GetMapping("/export")
	public void export(HttpServletResponse response) {
		//获取用户数据
		//将用户对象数据 拷贝到 UserVO 对象中
		List<UserVO> userVOList = userService.list().stream().map(user -> {
			UserVO userVO = new UserVO();
			BeanUtils.copyProperties(user, userVO);
			return userVO;
		}).collect(Collectors.toList());

		//设置编码格式
		response.setCharacterEncoding("utf-8");
		//设置导出文件名称（避免乱码）
		String fileName = URLEncoder.encode("用户数据列表".concat(".xlsx"), "UTF-8");
		//设置内容类型
		response.setHeader("content-type", "application/octet-stream");
		//设置响应的编码格式
		response.setHeader("content-disposition", "attachment;filename=" + fileName);

		// 指定使用类 UserDataVO 去写到第一个sheet，sheet命名为 用户数据列表，写完文件流会自动关闭
		EasyExcel.write(response.getOutputStream(), UserVO.class).sheet("用户数据列表").doWrite(userVOList);
	}

	@SneakyThrows
	@GetMapping("/export2")
	public void export2(HttpServletResponse response) {
		//获取用户数据
		//将用户对象数据 拷贝到 UserVO 对象中
		List<UserVO> userVOList = userService.list().stream().map(user -> {
			UserVO userVO = new UserVO();
			BeanUtils.copyProperties(user, userVO);
			return userVO;
		}).collect(Collectors.toList());

		//模拟异常
		throw new RuntimeException("出现异常了");

		/*//设置编码格式
		response.setCharacterEncoding("utf-8");
		//设置导出文件名称（避免乱码）
		String fileName = URLEncoder.encode("用户数据列表".concat(".xlsx"), "UTF-8");
		//设置内容类型
		response.setHeader("content-type", "application/octet-stream");
		//设置响应的编码格式
		response.setHeader("content-disposition", "attachment;filename=" + fileName);

		// 指定使用类 UserDataVO 去写到第一个sheet，sheet命名为 用户数据列表，写完文件流会自动关闭
		EasyExcel.write(response.getOutputStream(), UserVO.class).sheet("用户数据列表").doWrite(userVOList);*/
	}

	@SneakyThrows
	@PostMapping("/import")
	public Result<String> importData(@RequestPart MultipartFile file) {
		//1.读取excel文件数据,获取数据
		List<UserVO> list = EasyExcel.read(file.getInputStream())
				.head(UserVO.class)
				.sheet()
				.doReadSync();

		//2.bean拷贝 UserVO->User
		List<User> userList = list.stream().map(userVO -> {
			User user = new User();
			BeanUtils.copyProperties(userVO, user);
			return user;
		}).collect(Collectors.toList());

		//3.将数据存入数据库中
		userService.saveBatch(userList);
		return Result.success();
	}

	@SneakyThrows
	@PostMapping("/import2")
	public Result<String> importData2(@RequestPart MultipartFile file) {
		//读取excel文件数据,获取数据
		List<UserVO> list = EasyExcel.read(file.getInputStream())
				.registerReadListener(new UserCommonListener())
				.head(UserVO.class)
				.sheet()
				.doReadSync();

		//bean拷贝
		List<User> userList = list.stream().map(userVO -> {
			User user = new User();
			BeanUtils.copyProperties(userVO, user);
			return user;
		}).collect(Collectors.toList());

		//将数据存入数据库中
		userService.saveBatch(userList);
		return Result.success();
	}

}
